package com.fzb.forum4j.dao.impl;

import java.util.Map;
import java.util.Map.Entry;

import org.springframework.stereotype.Repository;

import com.fzb.forum4j.dao.api.IArticleDao;
import com.fzb.forum4j.po.Article;
import com.fzb.forum4j.po.Category;
import com.fzb.forum4j.po.QueryMode;
@Repository("articleDaoImpl")
public class ArticleDaoImpl extends HibBaseImpl<Article> implements IArticleDao {

	private static String BASICARTICLEMSG="t.user.details.headerFileName as header,t.title as title,t.category.categoryName as categoryName,t.views as view," +
			"t.comments.size as commentSize,t.category.catalog as catalog,t.user.userName as userName,t.pubTime as pubTime,t.cid as id";
	
	@Override
	public void getInitData(QueryMode<Article> qmd) {
		 CommonsQuery(qmd, "select new map("+BASICARTICLEMSG+") from Article t where t.category.catalog='"+ qmd.getT().getCategory().getCatalog()+"' order by ", "t", "t.cid",qmd.getT().getCategory());
	}
	@Override
	public void getEntityByEid(QueryMode<Article> qmd) {
		Map<String,String> article=(Map<String, String>) getSession().createQuery("select new map(t.content as content,"+BASICARTICLEMSG+") from Article t where t.cid=:cid and t.category.catalog=:catalog")
				.setParameter("cid", qmd.getId()).setParameter("catalog", qmd.getT().getCategory().getCatalog())
				.uniqueResult();
		if(article==null){
			qmd.getData().put("404", "内容不存在");
			return;
		}
		qmd.getData().putAll(article);
		Map<String, Object> next=(Map<String, Object>) getSession().createQuery("select new map(t.cid as id,t.title as title) from Article t where t.cid>:cid and t.category.catalog=:catalog")
				.setParameter("cid",qmd.getId()).setParameter("catalog", qmd.getT().getCategory().getCatalog()).setMaxResults(1).uniqueResult();
		Map<String, Object> last=(Map<String, Object>) getSession().createQuery("select new map(t.cid as id,t.title as title) from Article t where t.cid<:cid and t.category.catalog=:catalog order by t.cid desc")
				.setParameter("cid", qmd.getId()).setParameter("catalog", qmd.getT().getCategory().getCatalog()).setMaxResults(1).uniqueResult();
		updateAlias(qmd,last,next);
	}
	
	
	@Override
	public void getList(QueryMode<Article> qmd) {
		if(qmd.getT().getCategory()!=null){
			if(qmd.getT().getCategory().getCategroyId()!=null){
				QueryDataByQmd(qmd, BASICARTICLEMSG, "t.category="+qmd.getT().getCategory().getCategroyId()+" order by t.cid desc", "cid");
			}
			else
			{
				QueryDataByQmd(qmd, BASICARTICLEMSG, "t.category.catalog=:catalog order by t.cid desc", "cid", qmd.getT().getCategory());
			}
			
		}
		else{
			if(qmd.getFilters()!=null && qmd.getFilters().toString().length()>0){
				QueryDataByQmd(qmd, BASICARTICLEMSG, qmd.getFilters()+" order by t.cid desc", "cid");
			}
			else{
				QueryDataByQmd(qmd, BASICARTICLEMSG, "1=1 order by t.cid desc", "cid");
			}
		}
	}
	@Override
	public Category loadCategoryByCatalog(String catalog) {
		return (Category) getSession().createQuery("from Category t where t.catalog=:catalog").setParameter("catalog", catalog).uniqueResult();
	}
	@Override
	public void getArticleByKeywrod(QueryMode<Article> qmd) {
		QueryDataByQmd(qmd, BASICARTICLEMSG, "t.keywords like:keywords order by t.cid desc", "cid", qmd.getT());
	}
	@Override
	public void updateArticleView(Map<String, Integer> map) {
		for (Entry<String, Integer> m : map.entrySet()) 
		{
			Integer i=(Integer) getSession().createQuery("select t.views from Article t where t.cid=:cid")
					.setParameter("cid", Integer.parseInt(m.getKey())).uniqueResult();
			
			getSession().createQuery("update Article t set t.views=:view where t.cid=:cid")
					.setParameter("view", i+m.getValue())
					.setParameter("cid", Integer.parseInt(m.getKey())).executeUpdate();
		}
	}
	
	public static String getBASICARTICLEMSG(){
		return BASICARTICLEMSG;
	}

}
 